<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ include file="/pages/common/header.jsp"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Second Challan</title>
<script>
	function calculateAmount(count) {
		//var grandTotal=document.getElementById("grandTotal");
		var difference = document.getElementById("difference" + count).innerHTML;
		var rate = document.getElementById("landingCost" + count).value;
		var amount = difference * rate;
		var roundOffAmt = Math.round(amount * 100) / 100

		document.getElementById("amount" + count).innerHTML = roundOffAmt;

		var table = document.getElementById("secondChallanTable");

		var rowCount = table.rows.length;
		var sum = parseInt(0);
		rowCount = rowCount - 3;

		for ( var count = 1; count <= rowCount; count++) {
			var amount = document.getElementById("amount" + count).innerHTML;

			if (!amount == null || !amount == "")
				sum = sum + parseFloat(amount);
		}

		var roundOffgrandTotal = Math.round(sum * 100) / 100
		var grandTotal = document.getElementById("grandTotal");
		grandTotal.innerHTML = roundOffgrandTotal;
	}

	function getSlipNo() {
		var table = document.getElementById("secondChallanTable");

		var rowCount = table.rows.length;
		rowCount = rowCount - 3;
		var challanIds = "";
		for ( var count = 1; count <= rowCount; count++) {
			var slipNo = document.getElementById("slipNo" + count).innerHTML;
			if (!slipNo == null || !slipNo == "")
				challanIds = challanIds + slipNo + ","
		}

		var setIds = document.getElementById("challan2Ids");
		setIds.value = challanIds;
	}
</script>
</head>
<body class="mainbody">
	<c:set var="data" value="${requestScope.trader}" />
	<c:set var="grossWeightTotal" value="${requestScope.grossWeightTotal}" />
	<c:set var="finalDifference" value="0" />
	<c:set var="data" value="${requestScope.data}" />
	
	<c:set var="datefrmFirstChallan" value="${requestScope.dateInString}" />
	<c:set var="raBillNo" value="${requestScope.raBillNo}" />
	<c:set var="netWeightTotal" value="${requestScope.netWeightTotal}" />
	
	<c:if test="${not empty fn:length(tradersList)}">
		<c:forEach var="traders" items="${tradersList}">
			<c:set var="mulFactor" value="${traders.mulFactor}"></c:set>
			<c:set var="ratePerMT" value="${traders.landingCost}"></c:set>
			<c:set var="diffPerTonn" value="${traders.desc}"></c:set>
			<c:set var="lossQuanMulFactor" value="${traders.extraField1}"></c:set>
		</c:forEach>
	</c:if>
	<div class="mainContainer">
		<section class="bodyContainerChallan2">
		<form id="billTableForm" action="${context}/challan3" method="post">
			<div id="selectTraders" class="">
				<div class="divclear">
					<span><h1 style="text-align: center; font-weight: bold; font-size: 22px;">${trader}</h1>
					</span>
				</div>
				<p class="spacerTopTen"></p>
				<div class="divclear">
					<h4 style="text-align: center; font-weight: bold; font-size: 14px;">RECOVERY
						OF TRANSIT LOSS QUANTITY DURING TRANSPORT OF LIGNITE</h4>
				</div>
			</div>

			<div class="clear">
				<fmt:formatDate var="dateInString" type="date" value="${date}" />
				<h4 style="text-align: left; font-weight: bold; font-size: 14px;">
					RA.BILL NO.: <input type="text" class="billproddLable"
						name="raBillNo" id="billNo" value="${raBillNo}" />
				</h4>
				
				<h4 style="text-align: left; font-weight: bold; font-size: 14px;">
					Date: <input type="text" value="${datefrmFirstChallan}"
						class="billproddLable" name="dateInString" id="dateInString" />
				</h4>
			</div>
			<p class="hrlineBr"></p>
			<div id="secondChallan" class="">
				<div class="productDtls">
					<p class="spacerTopTen"></p>
					<div class="divclear">
						<span class="valia2">VALIA MINES SIDE</span> <span class="vastan2">VASTAN
							SIDE</span>
					</div>
					<div class="divclear">
						<table cellpadding="0" cellspacing="0" border="1px solid #333"
							class="display reportTableSecChalan" id="secondChallanTable"
							style="width: 982px">
							<thead>
								<!-- <tr>
											<th colspan="3"></th>
											<th colspan="7">VALIA MINES SIDE</th>
											<th colspan="6">VASTAN SIDE</th>
											<th colspan="3"></th>
										</tr> -->
								<tr>
									<th width="20">Sr.No.</th>
									<th width="">SLIP NO</th>
									<th width="">TRUCK NO</th>
									<th width="">SUB<br> TRANS<br> PORTER<br>
										NAME</th>
									<th width="">IN W.B. NO.</th>

									<th width="">IN DATE</th>
									<th width="">IN TIME</th>
									<th width="">TARE WEIGHT</th>
									<th width="">GROSS WEIGHT</th>
									<th width="">NET WEIGHT</th>

									<th width="">INVOICE NO</th>
									<th width="">ROYALTY<br> PASS NO.</th>
									<th width="">GROSS WEIGHT</th>
									<th width="">TARE WEIGHT</th>
									<th width="">NET WEIGHT</th>

									<th width="">DUMP LOCATION</th>
									<th width="">Difference</th>
									<th width="">cost<br> Rate<br> Per<br> M.T<br>
										In<br> Rs.</th>
									<th width="">Amt<br> In Rs.<br> ( Rounded/<br>Paisa)</th>
								</tr>
							</thead>
							<tbody>
								<c:if test="${data ne null && not empty data}">
									<c:forEach var="record" items="${data}" varStatus="count">
										<c:set var="differ"
											value="${record.vmsTotalWeight-record.vsNetWeight}" />
										<!-- If Difference is greater than trader difference then show it -->
										<c:if test="${differ gt diffPerTonn || differ eq diffPerTonn}">
											<c:set var="counts" value="${count.index+1}"></c:set>
											<c:if test="${counts eq 1}">
												<c:set var="dateTOfrd" value="${record.vmsInDate}"></c:set>
											</c:if>
										
										<tr>
											<td width="20">${counts}</td>
											<td width="50" id="slipNo${counts}">${record.slipNo}</td>
											<td width="50">${record.truckNo}</td>
											<td width="70">${record.subTransporterName}</td>
											<td width="25">${record.vmsInWbNo}</td>

											<td>${record.vmsInDate}</td>
											<td>${record.vmsInTime}</td>
											<td>${record.vmsTareWeight}</td>
											<td>${record.vmsGrossWeight}</td>
											<td>${record.vmsTotalWeight}</td>

											<td>${record.invoiceNo}</td>
											<td>${record.royalityPassNo}</td>

											<td>${record.vsGrossWeight}</td>
											<td>${record.vsTareWeight}</td>
											<td>${record.vsNetWeight}</td>
											<td>${record.dumpLocation}</td>
													<td id="difference${counts}"><fmt:formatNumber
															pattern="0.000" value="${differ}" />
													</td>
											<c:set var="finalDifference"
												value="${finalDifference + differ}" />
											<%-- <td><input type="text" class="billproddLable" name="landingCost" id="landingCost${counts}" onkeyup="calculateAmount(${counts});"/></td> --%>
											<td>${lossQuanMulFactor}</td>
											<td id="amount${counts}"><fmt:formatNumber
													pattern="0.000" value="${differ * mulFactor * lossQuanMulFactor}" />
											</td>
											<c:set var="grandtTotal"
												value="${grandtTotal+(differ * mulFactor * lossQuanMulFactor)}"></c:set>
										</tr>
										</c:if>
									</c:forEach>
								</c:if>
								<tr>
									<td colspan="15"></td>

									<td>Total</td>
									<td><fmt:formatNumber pattern="0.000"
											value="${finalDifference}" /></td>
									<td></td>
									<td><span id="grandTotal"><fmt:formatNumber
												pattern="0.000" value="${grandtTotal}" /> </span></td>

								</tr>

							</tbody>
						</table>
					</div>

					<p class="spacerTopTen"></p>
					<p class="spacerTopTen"></p>
				</div>

				<input type="hidden" name="challan2Ids" id="challan2Ids" /> <input
					type="hidden" name="ACTION" value="THRC"> <input
					type="hidden" name="grossWeightTotal" value="${grossWeightTotal}">
					<input type="hidden" name="netWeightTotal" value="${netWeightTotal}">
				<input type="hidden" name="finalDifference"
					value="${finalDifference}"> <input type="hidden"
					name="traderId" value="${requestScope.traderId}"> <input
					type="hidden" name="recordIds" value="${requestScope.recordIds}">
				<input type="hidden" name="dateTOfrd" value="${dateTOfrd}">
				<div id="buttonShow">
					<div id="buttonsHide" class="divclear">
						<span class="buttonSiyora button80"> <a href=""><input
								type="submit" onclick="getSlipNo();" value="Submit" /> </a> </span> <span
							class="buttonSiyora button80"
							onclick="printSecondChallan();AfterSecondPrint()"><a>Print</a>
						</span>
						<span class="buttonSiyora button80"><a href="${context}/pages/jsp/welcome.jsp" >Home </a> </span>
					</div>
				</div>
			</div>
		</form>
		</section>
	</div>

</body>
</html>